package com.gxg.dao;

import com.gxg.entities.ExperimentalEnvironment;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class ExperimentalEnvironmentDao
{
  @Autowired
  private JdbcTemplate jdbcTemplate;
  
  public void createExperimetalEnvironment(ExperimentalEnvironment experimentalEnvironment)
  {
    String sql = "insert into ExperimentalEnvironment values(?, ?, ?, ?, ?, ?,?)";
    this.jdbcTemplate.update(sql, new Object[] { experimentalEnvironment.getId(), experimentalEnvironment.getName(), experimentalEnvironment.getStatus(), experimentalEnvironment.getDocument(), Double.valueOf(experimentalEnvironment.getSize()), experimentalEnvironment.getCreateTime(), experimentalEnvironment.getImageId() });
  }
  
  public int getExperimentalEnvironmentCountByDocument(String documentId)
  {
    String sql = "select count(*) from ExperimentalEnvironment where document=?";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class, new Object[] { documentId })).intValue();
    return rowCount;
  }
  
  public List<ExperimentalEnvironment> getExperimentalEnviromentByDocument(String documentId)
  {
    if (getExperimentalEnvironmentCountByDocument(documentId) == 0) {
      return null;
    }
    String sql = "select * from ExperimentalEnvironment where document=?";
    List<ExperimentalEnvironment> experimentalEnvironmentList = this.jdbcTemplate.query(sql, new RowMapper()
    {
      public ExperimentalEnvironment mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        ExperimentalEnvironment experimentalEnvironment = new ExperimentalEnvironment();
        experimentalEnvironment.setId(resultSet.getString("id"));
        experimentalEnvironment.setName(resultSet.getString("name"));
        experimentalEnvironment.setStatus(resultSet.getString("status"));
        experimentalEnvironment.setDocument(resultSet.getString("Document"));
        experimentalEnvironment.setSize(resultSet.getDouble("size"));
        experimentalEnvironment.setCreateTime(resultSet.getTimestamp("createTime"));
        experimentalEnvironment.setImageId(resultSet.getString("imageId"));
        return experimentalEnvironment;
      }
    }, new Object[] { documentId });
    
    return experimentalEnvironmentList;
  }
  
  public int getExperimentalEnvironmentCountById(String id)
  {
    String sql = "select count(*) from ExperimentalEnvironment where id=?";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class, new Object[] { id })).intValue();
    return rowCount;
  }
  
  public ExperimentalEnvironment getExperimentalEnvironmentById(String id)
  {
    if (getExperimentalEnvironmentCountById(id) == 0) {
      return null;
    }
    String sql = "select * from ExperimentalEnvironment where id=?";
    ExperimentalEnvironment experimentalEnvironment = (ExperimentalEnvironment)this.jdbcTemplate.queryForObject(sql, new RowMapper()
    {
      public ExperimentalEnvironment mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        ExperimentalEnvironment experimentalEnvironment = new ExperimentalEnvironment();
        experimentalEnvironment.setId(resultSet.getString("id"));
        experimentalEnvironment.setName(resultSet.getString("name"));
        experimentalEnvironment.setStatus(resultSet.getString("status"));
        experimentalEnvironment.setDocument(resultSet.getString("Course"));
        experimentalEnvironment.setSize(resultSet.getDouble("size"));
        experimentalEnvironment.setCreateTime(resultSet.getTimestamp("createTime"));
        experimentalEnvironment.setImageId(resultSet.getString("imageId"));
        return experimentalEnvironment;
      }
    }, new Object[] { id });
    
    return experimentalEnvironment;
  }
  
  public void updateStatusById(String status, String id)
  {
    String sql = "update ExperimentalEnvironment set status=? where id=?";
    this.jdbcTemplate.update(sql, new Object[] { status, id });
  }
  
  public void deleteExperimentalEnvironmentById(String id)
  {
    String sql = "delete from ExperimentalEnvironment where id=?";
    this.jdbcTemplate.update(sql, new Object[] { id });
  }
}
